package com.yft.controller.login;

import com.yft.dao.base.BaseConstant;
import com.yft.dao.base.BaseUtils;
import com.yft.entity.base.Order;
import com.yft.entity.base.OrderSort;
import com.yft.entity.base.Pager;
import com.yft.entity.base.PagerImpl;
import com.yft.entity.login.SysMember;
import com.yft.service.login.SysMemberService;
import com.yft.util.MD5Encrypt;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.math.NumberUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;


@Controller
public class SysMemberController {
	private static final String controllerPath = "/yft/sysMember/";
//	private String viewpath = "/admin/base/jsp/sysMemberManage/";
	private String jsonViewpath = "/admin/sys_member/jsp/json/";
	
	@Autowired(required = true)
	private SysMemberService sysMemberService;

	private boolean checkNullBlank(Object[] array){
		if(array==null || array.length==0){
			return false;
		}
		for(int i=0;i<array.length;i++){
			if(array[i] != null && !"".equals(((String)array[i]).trim())){
				continue;
			}else{
				return false;
			}
		}
		return true;
	}
	@RequestMapping(controllerPath + "getSysMemberList.html")
	public String getAgentInfoListJson(HttpServletRequest request,HttpServletResponse response, ModelMap model) {
		SysMember condition = new SysMember();
		Pager pager = new PagerImpl();
		String roleName = StringUtils.trimToEmpty(request.getParameter("roleName"));
		String limit = request.getParameter("limit");
		if(roleName!=null && roleName.length()>0){
			condition.setSe_realName(roleName+"%");
		}
		int pageSize = 7;
		if (limit != null && limit.length() > 0)
			pageSize = NumberUtils.toInt(limit) > 0 ? NumberUtils.toInt(limit) : pageSize;
		String start = request.getParameter("start");
		int page = 1;
		if (start != null && start.length() > 0)
			page = NumberUtils.toInt(start) / pageSize + 1;
		pager.setPageSize(pageSize);
		pager.setPage(page);
		Order order = new Order();
		order.addOrder("createTime", OrderSort.DESC);
		
		List<SysMember> sysMemberList =  sysMemberService.getSysMemberList(condition, order, pager);
		
		model.addAttribute("totalProperty",pager.getRecordCount());
		model.addAttribute("sysMemberList",sysMemberList);
		
		return jsonViewpath + "sysMemberListJson";
	}
	@RequestMapping(controllerPath + "addSysMember.html")
	public String addSysMember(HttpServletRequest request,HttpServletResponse response, ModelMap model) throws Exception {
		String loginId = StringUtils.trimToEmpty(request.getParameter("loginId"));
		String password = StringUtils.trimToEmpty(request.getParameter("password"));
		String name = StringUtils.trimToEmpty(request.getParameter("name"));
		String email = StringUtils.trimToEmpty(request.getParameter("email"));
		String mobile = StringUtils.trimToEmpty(request.getParameter("mobile"));
		String sex = StringUtils.trimToEmpty(request.getParameter("sex"));
		String telzone = StringUtils.trimToEmpty(request.getParameter("telzone"));
		String telphone = StringUtils.trimToEmpty(request.getParameter("telphone"));
		String isWarehouse = StringUtils.trimToEmpty(request.getParameter("isWarehouse"));
        String warehouseId = StringUtils.trimToEmpty(request.getParameter("warehouseId"));
        String warehouseName = StringUtils.trimToEmpty(request.getParameter("warehouseName"));
        String warehouseCode = StringUtils.trimToEmpty(request.getParameter("warehouseCode"));
		boolean flag = false;
		String error = "";
		boolean judgePara = true;
		if((telzone==null || telzone.length()==0) && (telphone!=null && telphone.length()>0)){
			error ="电话号码前的区号不能为空";
			judgePara = false;
		}
		if((mobile==null || mobile.length()==0) && (telphone==null || telphone.length()==0)){
			error = "电话号码和手机号码不能同时为空";
			judgePara = false;
		}
		if(judgePara && checkNullBlank(new Object[]{loginId,password,name,email,sex})){
			SysMember sysMember = new SysMember();
			sysMember.setLoginID(loginId);
			sysMember.setPassword(MD5Encrypt.EncodeAsHex(password));
			sysMember.setRealName(name);
			sysMember.setLoginSum(0);
			sysMember.setSex(sex);
			sysMember.setEmail(email);
			if(mobile!=null && mobile.length()>0){
				sysMember.setMobile(mobile);
			}
			if(telzone!=null && telzone.length()>0){
				sysMember.setTelCode(telzone);
			}
			if(telphone!=null && telphone.length()>0){
				sysMember.setTel(telphone);
			}
            if(judgePara){
                if(sysMemberService.addSysMember(sysMember)!=null){
                    flag = true;
                }else{
                    error = "添加失败";
                }
            }
		}else{
			error = "请将信息补全";
		}
		model.addAttribute("success", flag);
		model.addAttribute("error", error);
		return "message/messageJson";
	}
	@RequestMapping(controllerPath + "modifySysMember.html")
	public String modifySysMember(HttpServletRequest request,HttpServletResponse response, ModelMap model) throws Exception {
		String id = StringUtils.trimToEmpty(request.getParameter("id"));
		String password = StringUtils.trimToEmpty(request.getParameter("password"));
		String name = StringUtils.trimToEmpty(request.getParameter("realName"));
		String email = StringUtils.trimToEmpty(request.getParameter("email"));
		String mobile = StringUtils.trimToEmpty(request.getParameter("mobile"));
		String sex = StringUtils.trimToEmpty(request.getParameter("sex"));
		String telzone = StringUtils.trimToEmpty(request.getParameter("telCode"));
		String telphone = StringUtils.trimToEmpty(request.getParameter("tel"));
		String isWarehouse = StringUtils.trimToEmpty(request.getParameter("isWarehouse"));
		String warehouseId = StringUtils.trimToEmpty(request.getParameter("warehouseId"));
		String warehouseName = StringUtils.trimToEmpty(request.getParameter("warehouseName"));
		String warehouseCode = StringUtils.trimToEmpty(request.getParameter("warehouseCode"));
		boolean flag = false;
        String customStr = null;
		String error = "";
		boolean judgePara = true;
		if((telzone==null || telzone.length()==0) && (telphone!=null && telphone.length()>0)){
			error ="电话号码前的区号不能为空";
			judgePara = false;
		}
		if((mobile==null || mobile.length()==0) && (telphone==null || telphone.length()==0)){
			error = "电话号码和手机号码不能同时为空";
			judgePara = false;
		}
		Integer sysMemberId = NumberUtils.toInt(id, -1);
		if(judgePara && checkNullBlank(new Object[]{name,email,sex}) && sysMemberId!=-1){
			SysMember sysMember = new SysMember();
			sysMember.setSysMemberId(sysMemberId);
			if(password!=null && password.length()>0)
				sysMember.setPassword(MD5Encrypt.EncodeAsHex(password));
			sysMember.setRealName(name);
			sysMember.setSex(sex);
			sysMember.setEmail(email);
			if(mobile!=null && mobile.length()>0){
				sysMember.setMobile(mobile);
			}else{
				sysMember.setMobile("");
			}
			if(telzone!=null && telzone.length()>0){
				sysMember.setTelCode(telzone);
			}else{
				sysMember.setTelCode("");
			}
			if(telphone!=null && telphone.length()>0){
				sysMember.setTel(telphone);
			}else{
				sysMember.setTel("");
			}
            if(judgePara){
                if(sysMemberService.updateValues(sysMember,customStr)){
                    flag = true;
                }else{
                    error = "修改失败";
                }
            }
		}else{
			error = "请将信息补全";
		}
		model.addAttribute("success", flag);
		model.addAttribute("error", error);
		return "message/messageJson";
	}
}
